package net.melon.product.inst.struts2;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import net.esj.basic.utils.Validators;
import net.esj.basic.utils.view.BaseJsonMessage.MessageLevel;
import net.esj.basic.utils.writer.WriterCode;
import net.esj.basic.view.AbstractPluginPageAction;
import net.melon.product.inst.service.BrandService;
import net.melon.product.inst.service.SpecificationsService;
import net.melon.product.inst.service.TypeService;
import net.melon.product.pojo.ProductAttribute;
import net.melon.product.pojo.ProductAttribute.ATTRIBUTE_REQUIRED;
import net.melon.product.pojo.ProductAttribute.ATTRIBUTE_TYPE;
import net.melon.product.pojo.ProductAttribute.ATTRIBUTE_UNIT;
import net.melon.product.pojo.ProductBrand;
import net.melon.product.pojo.ProductSpecifications;
import net.melon.product.pojo.ProductType;
import net.melon.product.pojo.ProductTypeBrandLink;
import net.melon.product.pojo.ProductTypeSpecificationsLink;
import net.melon.sysres.pojo.SysValue;

public class TypeAction extends AbstractPluginPageAction<ProductType, ProductType>{

	/**
	 * 
	 */
	private static final long serialVersionUID = -5644053942871460985L;

	@Resource
	private TypeService typeService;
	
	@Resource
	private BrandService brandService;
	
	@Resource
	private SpecificationsService specificationsService;
	
	private List<ProductAttribute> attributes = new ArrayList<ProductAttribute>();
	
	private List<ProductBrand> brands = new ArrayList<ProductBrand>();
	
	private List<ProductTypeBrandLink> brandLinks = new ArrayList<ProductTypeBrandLink>();
	
	private List<ProductSpecifications> specifications = new ArrayList<ProductSpecifications>();
	
	private List<ProductTypeSpecificationsLink> specificationsLinks = new ArrayList<ProductTypeSpecificationsLink>();
	
	@Override
	protected void doQueryPage() {
		pagination.getQueryBuilder().eq("state", 1);
		super.doQueryPage();
	}
	
	public String attribute(){
		return SUCCESS;
	}
	
	public String specifications(){
		specificationsLinks = typeService.exitSpecificationsLinks(Long.valueOf(id));
		specifications = specificationsService.specificationsLink();
		return SUCCESS;
	}
	
	public void addProductTypeSpecificationsLink(){
		try {
			typeService.addProductTypeSpecificationsLink(specificationsLinks,Long.valueOf(id));
			writeBaseJsonMessage(getText(SYSTEM_MESSAGE_TITLE,""), addSuccMsg(), MessageLevel.INFO, "html");
		} catch (Exception e) {
			e.printStackTrace();
			writeError(e.getMessage());
		}
	}
	
	public String brand(){
		brandLinks = typeService.existBrandLinks(Long.valueOf(id));
		brands = brandService.brandList();
		return SUCCESS;
	}
	
	public void addProductTypeBrandLink(){
		try {
			typeService.addProductTypeBrandLink(brandLinks,Long.valueOf(id));
			writeBaseJsonMessage(getText(SYSTEM_MESSAGE_TITLE,""), addSuccMsg(), MessageLevel.INFO, "html");
		} catch (Exception e) {
			e.printStackTrace();
			writeError(e.getMessage());
		}
	}
	
	public void getAttributeList(){
		try {
			List<ProductAttribute> list = typeService.getAttributeListByTypeId(Long.valueOf(id));
			writeJson(list,WriterCode.LIST);
		} catch (Exception e) {
			e.printStackTrace();
			writeError(e.getMessage());
		}
	}
	
	public void addAttributeList(){
		try {
			typeService.addAttributeList(attributes,Long.valueOf(id));
			writeBaseJsonMessage(getText(SYSTEM_MESSAGE_TITLE,""), addSuccMsg(), MessageLevel.INFO, "html");
		} catch (Exception e) {
			e.printStackTrace();
			writeError(e.getMessage());
		}
		
	}
	
	@Override
	protected void doGetEntity() {
		entity = defaultService.get(Long.valueOf(id), ProductType.class);
	}
	
	@Override
	protected void doDelete() throws Exception {
		typeService.deleteProductType(Long.valueOf(id));
	}
	public ProductType getEntity(){
		return entity;
	}
	
	public void setEntity(ProductType entity){
		this.entity = entity;
	}
	
	public void getTypes(){
		if(Validators.isEmpty(ProductAttribute.TYPES)){
			ATTRIBUTE_TYPE[] types = ProductAttribute.ATTRIBUTE_TYPE.values();
			for(ATTRIBUTE_TYPE type : types){
				SysValue value = new SysValue(String.valueOf(type.getType()),getDatagridTitle("type."+type.name().toString()));
				ProductAttribute.TYPES.add(value);
			}
		}
		writeJson(ProductAttribute.TYPES);
	}
	public void getUnits(){
		if(Validators.isEmpty(ProductAttribute.UNITS)){
			ATTRIBUTE_UNIT[] units = ProductAttribute.ATTRIBUTE_UNIT.values();
			for(ATTRIBUTE_UNIT unit : units){
				SysValue value = new SysValue(String.valueOf(unit.getUnit()),getDatagridTitle("unit."+unit.name().toString()));
				ProductAttribute.UNITS.add(value);
			}
		}
		writeJson(ProductAttribute.UNITS);
	}
	
	public void getRequireds(){
		if(Validators.isEmpty(ProductAttribute.REQUIREDS)){
			ATTRIBUTE_REQUIRED[] requireds = ProductAttribute.ATTRIBUTE_REQUIRED.values();
			for(ATTRIBUTE_REQUIRED required : requireds){
				SysValue value = new SysValue(String.valueOf(required.getRequired()),getDatagridTitle("required."+required.name().toString()));
				ProductAttribute.REQUIREDS.add(value);
			}
		}
		writeJson(ProductAttribute.REQUIREDS);
	}

	public List<ProductAttribute> getAttributes() {
		return attributes;
	}

	public void setAttributes(List<ProductAttribute> attributes) {
		this.attributes = attributes;
	}

	public List<ProductBrand> getBrands() {
		return brands;
	}

	public void setBrands(List<ProductBrand> brands) {
		this.brands = brands;
	}

	public List<ProductTypeBrandLink> getBrandLinks() {
		return brandLinks;
	}

	public void setBrandLinks(List<ProductTypeBrandLink> brandLinks) {
		this.brandLinks = brandLinks;
	}

	public List<ProductSpecifications> getSpecifications() {
		return specifications;
	}

	public void setSpecifications(List<ProductSpecifications> specifications) {
		this.specifications = specifications;
	}

	public List<ProductTypeSpecificationsLink> getSpecificationsLinks() {
		return specificationsLinks;
	}

	public void setSpecificationsLinks(
			List<ProductTypeSpecificationsLink> specificationsLinks) {
		this.specificationsLinks = specificationsLinks;
	}

}
